﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Utilities;
using FinanSoft.BusinessLayer;
using FinanSoft.Entities;

namespace FinanSoft
{
    public partial class frmFiltrosReporte : Form
    {
        /// <summary>
        /// Variables para enviar al fmrReporte
        /// </summary>
        Constants.ReportType tipoReporte;

        private int[] _periodIds;
        private int[] _seasonIds;

        public frmFiltrosReporte(Constants.ReportType tipoReporte_)
        {
            tipoReporte = tipoReporte_;
            InitializeComponent();
            fillPeriods();
            levelBox.SelectedIndex = 0;
        }

        private void fillPeriods()
        {
            cmbPeriodo.Items.Clear();
            AccountingPeriod[]periods = PeriodManager.GetInstance().GetAccountingPeriod();
            _periodIds = new int[periods.Length];
            for(int periodIndex = 0; periodIndex < periods.Length; periodIndex++)
            {
                cmbPeriodo.Items.Add(periods[periodIndex].Name);
                _periodIds[periodIndex] = periods[periodIndex].Id;
            }
            cmbPeriodo.SelectedIndex = cmbPeriodo.Items.Count - 1;
        }

        private void fillSeasons()
        {
            cmbTemporada.Items.Clear();
            AccountingPeriod selectedPeriod = PeriodManager.GetInstance().GetPeriod(_periodIds[cmbPeriodo.SelectedIndex]);
            _seasonIds = new int[selectedPeriod.Seasons.Length];
            for (int seasonIndex = 0; seasonIndex < selectedPeriod.Seasons.Length; seasonIndex++)
            {
                cmbTemporada.Items.Add(selectedPeriod.Seasons[seasonIndex].Name);
                _seasonIds[seasonIndex] = selectedPeriod.Seasons[seasonIndex].Id;
            }
            cmbTemporada.SelectedIndex = cmbTemporada.Items.Count - 1;
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAplicar_Click(object sender, EventArgs e)
        {
            //Indica el tipo de moneda
            int tipoMoneda;
            if (rdbLocal.Checked)
                tipoMoneda = MoneyManager.GetInstance().LocalCurrency.Id;
            else 
                tipoMoneda = MoneyManager.GetInstance().SystemCurrency.Id;
            bool isSeason = false;
            int timeId = Constants.INVALID_ID;
            //Indica el tipo de espacio temporal
            if (rdbPeriodo.Checked)
            {
                if (cmbPeriodo.SelectedIndex >= 0)
                {
                    timeId = _periodIds[cmbPeriodo.SelectedIndex];
                    isSeason = false;
                }
                else
                {
                    MessageBox.Show("Debe indicar algun periodo o temporada");
                    return;
                }
            }
            else if (rdbTemporada.Checked)
            {
                if (cmbTemporada.SelectedIndex >= 0)
                {
                    timeId = _seasonIds[cmbTemporada.SelectedIndex];
                    isSeason = true;
                }
                else
                {
                    MessageBox.Show("Debe indicar algun periodo o temporada");
                    return;
                }
            }
            // Cargamos y mostramos el formulario Reporte
            frmReporte frmReporteV = new frmReporte(tipoReporte, isSeason, timeId, tipoMoneda, Convert.ToInt32(levelBox.Text));
            frmReporteV.ShowDialog(this);     // Mostramos el formulario de forma modal.
            rdbPeriodo.Select();
            fillPeriods();
            cmbTemporada.Text = "";
        }

        private void rdbTemporada_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTemporada.Checked)
            {
                cmbPeriodo.Enabled = false;
                cmbTemporada.Enabled = true;
                fillSeasons();
            }
        }

        private void rdbPeriodo_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbPeriodo.Checked)
            {
                cmbTemporada.Enabled = false;
                cmbPeriodo.Enabled = true;
                cmbTemporada.Items.Clear();
                cmbTemporada.SelectedIndex = -1;
                cmbTemporada.Text = "";
            }
        }
    }
}
